package hd.util.widget.listview.pinned;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

import hd.util.R;
import hd.util.adapter.BaseViewHolder;

/**
 * Adapter
 * <p/>
 * Created by Haidy on 15/3/22.
 */
public abstract class HeaderAdapter<D, V> extends BaseAdapter implements PinnedHeaderAdapter, AbsListView.OnScrollListener {

    public static final int TYPE_CATEGORY_ITEM = 0;
    public static final int TYPE_ITEM = 1;

    @Override
    public int getPinnedHeaderState(int position) {
        return 0;
    }

    @Override
    public void configurePinnedHeader(View header, int position, int alpha) {

    }

    private final Context context;
    private List<D> dataList = new ArrayList<>();

    public HeaderAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    public void setData(@NonNull List<D> data) {
        dataList.clear();
        addAllData(data);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public void addData(D d) {
        dataList.add(d);
        notifyDataSetChanged();
    }

    public void addData(D t, int index) {
        dataList.add(index, t);
        notifyDataSetChanged();
    }

    public void addAllData(@NonNull List<D> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    /**
     * 移除一项
     *
     * @param index 位置
     */
    public void remove(int index) {
        if (dataList.size() >= index + 1)
            dataList.remove(index);
        notifyDataSetChanged();
    }

    public void clear() {
        dataList.clear();
        notifyDataSetChanged();
    }

    @Override
    public D getItem(int position) {
        if (dataList.size() <= position)
            return null;
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public abstract int getContentView();

    public abstract View getHeaderView();

    public abstract BaseViewHolder<D, V> getHolder();

    public abstract V initViewHolder(View convertView, int type);

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseViewHolder<D, V> holder;
        int type = getItemViewType(position);
        switch (type) {
            case TYPE_ITEM:
                if (convertView == null || convertView.getTag(R.id.score_type) != type) {
                    convertView = View.inflate(context, getContentView(), null);
                    V k = initViewHolder(convertView, type);
                    holder = getHolder();
                    holder.setHolder(k);
                    convertView.setTag(holder);
                    convertView.setTag(R.id.score_type, type);
                } else {
                    holder = (BaseViewHolder<D, V>) convertView.getTag();
                }
                break;
            case TYPE_CATEGORY_ITEM:
                if (convertView == null || convertView.getTag(R.id.score_type) != type) {
                    convertView = getHeaderView();
                    V k = initViewHolder(convertView, type);
                    holder = getHolder();
                    holder.setHolder(k);
                    convertView.setTag(holder);
                    convertView.setTag(R.id.score_type, type);
                } else {
                    holder = (BaseViewHolder<D, V>) convertView.getTag();
                }
                break;
            default:
                return convertView;
        }
        holder.loadData(holder.getHolder(), getItem(position), position);
        return convertView;
    }
}
